$(document).ready(function() {
		

	
	
	$(function() {
		$( "#tabs" ).tabs().addClass( "ui-tabs-vertical ui-helper-clearfix" );
		$( "#tabs li" ).removeClass( "ui-corner-top" ).addClass( "ui-corner-left" );
	});
	
	$(".ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all").append("<div id='bottom_box'></div>");
	
	$(".sectionarrow").click(function(){
		var SectionContent = $(this).parent().parent().find(".sectioncontent");
		if($(SectionContent[0]).css('display') == 'none')
		{
			$(SectionContent[0]).slideDown(100);
			$(this).removeClass('rotate');
		}
		else
		{
			$(SectionContent[0]).slideUp(100);
			$(this).addClass('rotate');
		}
	});
	
	$("#Calendar").datepicker(function (){
		
	});
	
	
	
	
	if($.trim($("#Power").html()) != '0' )
	{
	// Graph tool
	
		GetClassAverages();
        
	}
	
	var NewTestDialog = $("#dialog");

	$("#NewTestLink").click(function() {
		NewTestDialog.load('NewTestList.php', function() {
			NewTestDialog.dialog({
			  maxWidth:525,
				maxHeight: 1000,
				width: 525,
			  modal: true,
			  center: true,
			  position: 'center'
			});
			
		
		});
	});
	
	
	$("#UngradedTestLink").click(function() {
		NewTestDialog.attr("title","Ungraded Test");
		NewTestDialog.load('UngradedTestList.php', function() {
			NewTestDialog.dialog({
			  maxWidth:400,
				maxHeight: 1000,
				width: 400,
			  modal: true,
			  center: true,
			  position: 'center'
			});
			
		
		});
	});
	
});

function GetClassAverages()
{
	LoadingStart();
	$.ajax({
			 url: "webservices.php",
			 type: "POST",
			 data: { Func: 'Get_class_average' },
			 dataType: 'json',
			 success: function (data) {
				var averages = [];
				var classes = [];
				
				console.log(data);
				
				$(data).each(function() {
					
					averages.push(parseInt(this.GradeAvg == 'NULL' ? '0' : this.GradeAvg));
					classes.push(this.ClassName);
					
				});
				
				$.jqplot.config.enablePlugins = true;
				 
				plot1 = $.jqplot('chart1', [averages], {
					// Only animate if we're not using excanvas (not in IE 7 or IE 8)..
					animate: !$.jqplot.use_excanvas,
					seriesDefaults:{
						renderer:$.jqplot.BarRenderer,
						pointLabels: { show: true ,formatString: '%s%%' }
					},
					axes: {
						xaxis: {
							renderer: $.jqplot.CategoryAxisRenderer,
							ticks: classes
						}
					},
					highlighter: { show: false }
				}); 
				
				LoadingEnd();

			 },
			 error: function (e)
			 {
				console.log(e);
			 }
			 
	 });



}

function LoadingStart()
{
	$("#GradeOverview").find(".sectioncontent").append('<img src="images/loader.gif" id="LoaderBar" />');
}

function LoadingEnd()
{
	$("#GradeOverview").find(".sectioncontent").find('#LoaderBar').fadeOut();
}



